package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 5000,retries = 0)
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public Boolean importOrderSettings(List<String[]> readExcelList) {
        List<OrderSetting> orderSettingList= changeStringToList(readExcelList);
        //    1    2020-12-15   100  0
        //  根据日期判断数据库表里面是否已经存在   如果已经存在  该记录主键 赋值 OrderSetting
        for (OrderSetting orderSetting : orderSettingList) {
            QueryWrapper<OrderSetting> wrapper = new QueryWrapper<>();
            wrapper.eq("ORDERDATE",orderSetting.getOrderDate());
            OrderSetting orderSettingExistOrNot = baseMapper.selectOne(wrapper);
            if (orderSettingExistOrNot!=null){
                //  数据库存在该日期记录  查询当前记录id  赋值 OrderSetting对象  mp会自动执行 update
             orderSetting.setId(orderSettingExistOrNot.getId());
            }
        }
        //  数据库存在该日期记录  查询当前记录id  赋值 OrderSetting对象  mp会自动执行 update
        boolean flag = saveOrUpdateBatch(orderSettingList, 100);
        return flag;
    }

    @Override
    public Map<String, Object> findSettingData(int year, int month) {
        String beginDate=year+"-"+month+"-1";
        String endDate=year+"-"+month+"-31";
       List<OrderSetting> orderSettingList= baseMapper.findSettingDataByOrderDate(beginDate,endDate);
        Map<String, Object> map = new HashMap<>();
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting orderSettingMapValue = new OrderSetting();
            orderSettingMapValue.setNumber(orderSetting.getNumber());
            orderSettingMapValue.setReservations(orderSetting.getReservations());
            //数据库日期格式 yyyy-MM-dd
            map.put(DateUtils.parseDate2String(orderSetting.getOrderDate(),"yyyy-MM-dd"),orderSettingMapValue);
        }
        return map;
    }

    @Override
    public Boolean updateNumberByOrderDate(int number, String orderdate) {

        boolean flag= baseMapper.updateNumberByOrderDate(number,orderdate);
        return flag;
    }

    @Override
    @LcnTransaction
    public int isOkorNot(String orderDate) {
       int count= baseMapper.isOkorNot(orderDate);
       return count;
    }

    @Override
    @LcnTransaction
    public void updateReservationsByOrderDate(String orderDate) {
        baseMapper.updateReservationsByOrderDate(orderDate);
    }

    private List<OrderSetting> changeStringToList(List<String[]> readExcelList) {
        if (readExcelList!=null&&readExcelList.size()!=0){
            List<OrderSetting> list = new ArrayList<>();
            for (String[] strings : readExcelList) {
                OrderSetting orderSetting = new OrderSetting();
                //DateUtils 工具类中传入字符串必须：yyyy/MM/dd
                //excel表格日期也是yyyy/MM/dd
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setReservations(0);
                list.add(orderSetting);
            }
            return list;
        }
        return null;
    }
}
